/*
 * @Author: silence
 * @Date: 2023-08-28 11:44:12
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-09-18 15:14:36
 * @Description: 英臻配网实现
 * 
 */
import 'dart:convert';

import 'package:sil_utils_package/sil_utils_package.dart';
import 'package:sil_wifi_yz_plugin/sil_wifi_yz_plugin.dart';

import 'DeviceNetworkingInterface.dart';
import 'WifiDeviceBackData.dart';

/// 授权
const String YzUserName = "admin";
const String YzPwd = "admin";
String yzAuthorization() {
  List<int> code = Utf8Encoder().convert("$YzUserName:$YzPwd");
  String base64 = base64Encode(code).toString();
  return "Basic $base64";
}

class DeviceNetworkingInterfaceForYZ extends DeviceNetworkingInterface {
  DeviceNetworkingInterfaceForYZ() {}
  final _silWifiYZPlugin = SilWifiYzPlugin();

  @override
  Future<WifiDeviceBackData> checkDeiviceWifiConnectState() async {
    WifiDeviceBackData resultDeviceData = kFailDeviceData;

    Map deviceReturn =
        await _silWifiYZPlugin.requestInit(authorization: yzAuthorization()) ??
            Map();

    if (SilTools.mapIsEmpty(deviceReturn)) {
      return resultDeviceData;
    }

    resultDeviceData.code = deviceReturn['code'];
    resultDeviceData.data = deviceReturn['data'];
    resultDeviceData.msg = deviceReturn['msg'];
    return resultDeviceData;
  }

  @override
  Future<WifiDeviceBackData> deiviceSetHomeWifi(
      {required String ssid, required String pwd}) async {
    WifiDeviceBackData resultDeviceData = kFailDeviceData;

    Map deviceReturn = await _silWifiYZPlugin.setWifiNetBySsidAndPwd(
            authorization: yzAuthorization(),
            ssid: ssid,
            pwd: pwd,
            loginUsername: YzUserName,
            loginPwd: YzPwd) ??
        Map();

    if (SilTools.mapIsEmpty(deviceReturn)) {
      return resultDeviceData;
    }

    resultDeviceData.code = deviceReturn['code'];
    resultDeviceData.data = deviceReturn['data'];
    resultDeviceData.msg = deviceReturn['msg'];
    return resultDeviceData;
  }

  /// 重启准备中（第一步）
  Future<WifiDeviceBackData> api_yz_wifi_restart() async {
    WifiDeviceBackData resultDeviceData = kFailDeviceData;

    Map deviceReturn = await _silWifiYZPlugin.perferRestart(
            authorization: yzAuthorization()) ??
        Map();

    if (SilTools.mapIsEmpty(deviceReturn)) {
      return resultDeviceData;
    }

    resultDeviceData.code = deviceReturn['code'];
    resultDeviceData.data = deviceReturn['data'];
    resultDeviceData.msg = deviceReturn['msg'];
    return resultDeviceData;
  }

  /// 重启（第二步）
  @override
  Future<WifiDeviceBackData> deiviceRestart() async {
    WifiDeviceBackData resultDeviceData = kFailDeviceData;
    Map deviceReturn =
        await _silWifiYZPlugin.restart(authorization: yzAuthorization()) ??
            Map();

    if (SilTools.mapIsEmpty(deviceReturn)) {
      return resultDeviceData;
    }

    resultDeviceData.code = deviceReturn['code'];
    resultDeviceData.data = deviceReturn['data'];
    resultDeviceData.msg = deviceReturn['msg'];
    return resultDeviceData;
  }

  @override
  Future<WifiDeviceBackData> checkIsSuccess() async {
    WifiDeviceBackData resultDeviceData = kFailDeviceData;
    Map deviceReturn = await _silWifiYZPlugin.checkIsSuccess(
            authorization: yzAuthorization()) ??
        Map();

    if (SilTools.mapIsEmpty(deviceReturn)) {
      return resultDeviceData;
    }

    resultDeviceData.code = deviceReturn['code'];
    resultDeviceData.data = deviceReturn['data'];
    resultDeviceData.msg = deviceReturn['msg'];
    return resultDeviceData;
  }
}
